package com.lxmes.sales.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.convert.Convert;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.sales.domain.ZcQuotationOverview;
import com.lxmes.sales.domain.ZcSupplierQuotation;
import com.lxmes.sales.mapper.ZcQuotationOverviewMapper;
import com.lxmes.sales.mapper.ZcSupplierQuotationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.ZcQuotationDetailsMapper;
import com.lxmes.sales.domain.ZcQuotationDetails;
import com.lxmes.sales.service.IZcQuotationDetailsService;

/**
 * 报价明细Service业务层处理
 *
 * @author lx
 * @date 2023-06-21
 */
@Service
public class ZcQuotationDetailsServiceImpl implements IZcQuotationDetailsService
{
    @Autowired
    private ZcQuotationDetailsMapper zcQuotationDetailsMapper;

    @Autowired
    private ZcQuotationOverviewMapper zcQuotationOverviewMapper;
    @Autowired
    private ZcSupplierQuotationMapper zcSupplierQuotationMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询报价明细
     *
     * @param id 报价明细主键
     * @return 报价明细
     */
    @Override
    public ZcQuotationDetails selectZcQuotationDetailsById(Long id)
    {
        return zcQuotationDetailsMapper.selectZcQuotationDetailsById(id);
    }

    /**
     * 查询报价明细列表
     *
     * @param zcQuotationDetails 报价明细
     * @return 报价明细
     */
    @Override
    public List<ZcQuotationDetails> selectZcQuotationDetailsList(ZcQuotationDetails zcQuotationDetails)
    {
        return zcQuotationDetailsMapper.selectZcQuotationDetailsList(zcQuotationDetails);
    }

    /**
     * 新增报价明细
     *
     * @param zcQuotationDetails 报价明细
     * @return 结果
     */
    @Override
    public int insertZcQuotationDetails(ZcQuotationDetails zcQuotationDetails)
    {
        return zcQuotationDetailsMapper.insertZcQuotationDetails(zcQuotationDetails);
    }

    /**
     * 修改报价明细
     *
     * @param zcQuotationDetails 报价明细
     * @return 结果
     */
    @Override
    public int updateZcQuotationDetails(ZcQuotationDetails zcQuotationDetails)
    {
        return zcQuotationDetailsMapper.updateZcQuotationDetails(zcQuotationDetails);
    }

    /**
     * 批量删除报价明细
     *
     * @param ids 需要删除的报价明细主键
     * @return 结果
     */
    @Override
    public int deleteZcQuotationDetailsByIds(Long[] ids)
    {
        return zcQuotationDetailsMapper.deleteZcQuotationDetailsByIds(ids);
    }

    /**
     * 删除报价明细信息
     *
     * @param id 报价明细主键
     * @return 结果
     */
    @Override
    public int deleteZcQuotationDetailsById(Long id)
    {
        return zcQuotationDetailsMapper.deleteZcQuotationDetailsById(id);
    }
    @Override
    public String imporIteminfo(List<ZcQuotationDetails> zcQuotationDetailsList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(zcQuotationDetailsList) || zcQuotationDetailsList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZcQuotationDetails zcQuotationDetails : zcQuotationDetailsList)
        {
            try
            {
                BeanValidators.validateWithException(validator, zcQuotationDetails);
                zcQuotationDetails.setCreateBy(operName);
                zcQuotationDetails.setUpdateBy(operName);
                this.insertZcQuotationDetails(zcQuotationDetails);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int auditZcQuotationDetails(ZcQuotationDetails zcQuotationDetails, String audit) {
        if (audit.equals("1")) {
            ZcQuotationOverview zcQuotationOverview = Convert.convert(ZcQuotationOverview.class, zcQuotationDetails);
            zcQuotationOverview.setAuditDate(new Date());
            zcQuotationOverview.setAuditor(SecurityUtils.getUsername());
            zcQuotationOverview.setDeptId(SecurityUtils.getDeptId());
            zcQuotationOverviewMapper.insertZcQuotationOverview(zcQuotationOverview);
            return 1;
        }
        if (audit.equals("0")) {
            ZcSupplierQuotation zcSupplierQuotation =  Convert.convert(ZcSupplierQuotation.class, zcQuotationDetails);
            zcSupplierQuotation.setDeptId(SecurityUtils.getDeptId());
            zcSupplierQuotationMapper.insertZcSupplierQuotation(zcSupplierQuotation);
            return 1;
        }
        throw new ServiceException("参数错误");
    }
}
